http.client 您所在的位置:网站首页 h5 客户端 网页端 http.client

http.client

2023-06-11 01:38| 来源: 网络整理| 查看: 265

This will send a request to the server using the HTTP request method method and the request URI url. The provided url must be an absolute path to conform with RFC 2616 §5.1.2 (unless connecting to an HTTP proxy server or using the OPTIONS or CONNECT methods).

如果给定 body,那么给定的数据会在信息头完成之后发送。它可能是一个 字符串,一个 bytes-like object,一个打开的 file object,或者 bytes 迭代器。如果 body 是字符串,它会按 HTTP 默认的 ISO-8859-1 编码。如果是一个字节类对象,它会按原样发送。如果是 file object,文件的内容会被发送,这个文件对象应该至少支持``read()`` 方法。如果这个文件对象是一个 io.TextIOBase 实例,由 read() 方法返回的数据会按 ISO-8859-1 编码,否则由 read() 方法返回的数据会按原样发送。如果 body 是一个迭代器,迭代器中的元素会被发送,直到迭代器耗尽。

The headers argument should be a mapping of extra HTTP headers to send with the request. A Host header must be provided to conform with RFC 2616 §5.1.2 (unless connecting to an HTTP proxy server or using the OPTIONS or CONNECT methods).

如果 headers 既不包含 Content-Length 也没有 Transfer-Encoding,但存在请求正文,那么这些头字段中的一个会自动设定。如果 body 是 None,那么对于要求正文的方法 (PUT,POST,和 PATCH),Content-Length 头会被设为 0。如果 body 是字符串或者类似字节的对象,并且也不是 文件,Content-Length 头会设为正文的长度。任何其他类型的 body (一般是文件或迭代器)会按块编码,这时会自动设定 Transfer-Encoding 头以代替 Content-Length。

在 headers 中指定 Transfer-Encoding 时, encode_chunked 是唯一相关的参数。如果 encode_chunked 为 False,HTTPConnection 对象会假定所有的编码都由调用代码处理。如果为 True,正文会按块编码。

For example, to perform a GET request to https://docs.python.org/3/:

>>> import http.client >>> host = "docs.python.org" >>> conn = http.client.HTTPSConnection(host) >>> conn.request("GET", "/3/", headers={"Host": host}) >>> response = conn.getresponse() >>> print(response.status, response.reason) 200 OK

备注

HTTP 协议在 1.1 版中添加了块传输编码。除非明确知道 HTTP 服务器可以处理 HTTP 1.1,调用者要么必须指定 Content-Length,要么必须传入 str 或字节类对象,注意该对象不能是表达 body 的文件。

在 3.2 版本加入: body 现在可以是可迭代对象了。

在 3.6 版本发生变更: 如果 Content-Length 和 Transfer-Encoding 都没有在 headers 中设置,文件和可迭代的 body 对象现在会按块编码。添加了 encode_chunked 参数。不会尝试去确定文件对象的 Content-Length。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有